package dgut.party.business.dao.publication;

import dgut.party.business.common.enumeration.Priority;
import dgut.party.business.entity.publication.Publication;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Repository
public interface PublicationRepository extends JpaRepository<Publication, Long> {

//  @Query("select new dgut.party.business.dto.to.PubDetailDto(pub.content) from Publication pub where pub.id = :id")
//  Optional<Publication> findPubContentById(@Param("id") Long id);

  List<Publication> findByTitleLike(String title);

  List<Publication> findTop5ByPriorityAndCreateTimeLessThan(Date before, Priority priority);

  List<Publication> findTop5ByTypeAndPriorityAndCreateTimeLessThan(String type, Priority priority, Date before);

  // -------仅可用的start

  @Query("select new dgut.party.business.dto.to.PublicationDto(pub.id,pub.title,pub.priority, pub.summary,pub.createTime,pub.updateTime,pub.endTime,pub.type)" +
    " from Publication pub" +
    " where pub.disabled=false and (pub.title like :keyword or pub.summary like :keyword)")
  Page<Publication> findAllAvailableByKeywordPage(@Param("keyword") String keyword, Pageable pageable);

  @Query("select new dgut.party.business.dto.to.PublicationDto(pub.id,pub.title,pub.priority, pub.summary,pub.createTime,pub.updateTime,pub.endTime,pub.type)" +
    " from Publication pub" +
    " where pub.disabled=false and pub.type = :dtype and (pub.title like :keyword or pub.summary like :keyword)")
  Page<Publication> findAllAvailableByTypeAndKeywordPage(@Param("keyword") String keyword, @Param("dtype") String type, Pageable pageable);

  @Query("select new dgut.party.business.dto.to.PublicationDto(pub.id,pub.title,pub.priority, pub.summary,pub.createTime,pub.updateTime,pub.endTime,pub.type)" +
    " from Publication pub" +
    " where pub.disabled=false and pub.type = :dtype")
  Page<Publication> findAllAvailableByTypePage(@Param("dtype") String type, Pageable pageable);

  @Query("select new dgut.party.business.dto.to.PublicationDto(pub.id,pub.title,pub.priority, pub.summary,pub.createTime,pub.updateTime,pub.endTime,pub.type)" +
    " from Publication pub " +
    "where pub.disabled=false")
  Page<Publication> findAllAvailablePage(Pageable pageable);

  // -------仅可用的end


  @Query("select new dgut.party.business.dto.to.PublicationDto(pub.id,pub.title,pub.priority, pub.summary,pub.createTime,pub.updateTime,pub.endTime,pub.type)" +
    " from Publication pub" +
    " where pub.title like :keyword or pub.summary like :keyword")
  Page<Publication> findAllByKeywordPage(@Param("keyword") String keyword, Pageable pageable);

  @Query("select new dgut.party.business.dto.to.PublicationDto(pub.id,pub.title,pub.priority, pub.summary,pub.createTime,pub.updateTime,pub.endTime,pub.type)" +
    " from Publication pub" +
    " where pub.type = :dtype and (pub.title like :keyword or pub.summary like :keyword)")
  Page<Publication> findAllByTypeAndKeywordPage(@Param("keyword") String keyword, @Param("dtype") String type, Pageable pageable);

  @Query("select new dgut.party.business.dto.to.PublicationDto(pub.id,pub.title,pub.priority, pub.summary,pub.createTime,pub.updateTime,pub.endTime,pub.type)" +
    " from Publication pub where pub.type = :dtype")
  Page<Publication> findAllByTypePage(@Param("dtype") String type, Pageable pageable);

  @Query("select new dgut.party.business.dto.to.PublicationDto(pub.id,pub.title,pub.priority, pub.summary,pub.createTime,pub.updateTime,pub.endTime,pub.type)" +
    " from Publication pub")
  Page<Publication> findAllPage(Pageable pageable);

}
